package com.google.android.exoplayer2.extractor.mp4;

import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.metadata.Metadata;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import o.adv;
import o.adw;
import o.ady;
import o.adz;
import o.aea;
import o.aea.a;
import o.aec;
import o.aef;
import o.aeh;
import o.aeu;
import o.aeu.d;
import o.aez;
import o.afa;
import o.afd;
import o.afg;
import o.afi;
import o.amm;
import o.anc;
import o.and;
import o.anr;

public final class Mp4Extractor implements Extractor, aea {
    public static final adw ˊ = afd.ˏ;
    private static final int ˋ = anr.ᐝ("qt  ");
    private int ʻ;
    private ady ʻॱ;
    private long ʼ;
    private int ʽ;
    private boolean ʽॱ;
    private anc ˊॱ;
    private int ˋॱ;
    private final anc ˎ;
    private final anc ˏ;
    private int ˏॱ;
    private int ͺ;
    private final int ॱ;
    private int ॱˊ;
    private int ॱˋ;
    private e[] ॱˎ;
    private final ArrayDeque<o.aeu.e> ॱॱ;
    private long ॱᐝ;
    private final anc ᐝ;
    private long[][] ᐝॱ;

    static final class e {
        public int ˊ;
        public final aef ˋ;
        public final afi ˎ;
        public final Track ˏ;

        public e(Track track, afi o_afi, aef o_aef) {
            this.ˏ = track;
            this.ˎ = o_afi;
            this.ˋ = o_aef;
        }
    }

    public Mp4Extractor() {
        this(0);
    }

    public Mp4Extractor(int i) {
        this.ॱ = i;
        this.ᐝ = new anc(16);
        this.ॱॱ = new ArrayDeque();
        this.ˏ = new anc(and.ˏ);
        this.ˎ = new anc(4);
        this.ˋॱ = -1;
    }

    public boolean ˎ(adv o_adv) throws IOException, InterruptedException {
        return afg.ˊ(o_adv);
    }

    public void ˏ(ady o_ady) {
        this.ʻॱ = o_ady;
    }

    public void ˊ(long j, long j2) {
        this.ॱॱ.clear();
        this.ˏॱ = 0;
        this.ˋॱ = -1;
        this.ॱˊ = 0;
        this.ͺ = 0;
        if (j == 0) {
            ˋ();
        } else if (this.ॱˎ != null) {
            ˋ(j2);
        }
    }

    public void ॱ() {
    }

    public int ˎ(adv o_adv, adz o_adz) throws IOException, InterruptedException {
        while (true) {
            switch (this.ʻ) {
                case 0:
                    if (ˋ(o_adv)) {
                        break;
                    }
                    return -1;
                case 1:
                    if (!ˊ(o_adv, o_adz)) {
                        break;
                    }
                    return 1;
                case 2:
                    return ॱ(o_adv, o_adz);
                default:
                    throw new IllegalStateException();
            }
        }
    }

    public boolean ˊ() {
        return true;
    }

    public long g_() {
        return this.ॱᐝ;
    }

    public a ˏ(long j) {
        if (this.ॱˎ.length == 0) {
            return new a(aeh.ˎ);
        }
        long j2;
        long j3;
        long j4 = -9223372036854775807L;
        long j5 = -1;
        if (this.ॱˋ != -1) {
            afi o_afi = this.ॱˎ[this.ॱˋ].ˎ;
            int ˊ = ˊ(o_afi, j);
            if (ˊ == -1) {
                return new a(aeh.ˎ);
            }
            j2 = o_afi.ʻ[ˊ];
            j3 = o_afi.ˋ[ˊ];
            if (j2 < j && ˊ < o_afi.ˊ - 1) {
                int ˏ = o_afi.ˏ(j);
                if (!(ˏ == -1 || ˏ == ˊ)) {
                    j4 = o_afi.ʻ[ˏ];
                    j5 = o_afi.ˋ[ˏ];
                }
            }
            j = j2;
        } else {
            j3 = Long.MAX_VALUE;
        }
        j2 = j3;
        j3 = j5;
        for (int i = 0; i < this.ॱˎ.length; i++) {
            if (i != this.ॱˋ) {
                afi o_afi2 = this.ॱˎ[i].ˎ;
                j2 = ˏ(o_afi2, j, j2);
                if (j4 != -9223372036854775807L) {
                    j3 = ˏ(o_afi2, j4, j3);
                }
            }
        }
        aeh o_aeh = new aeh(j, j2);
        if (j4 == -9223372036854775807L) {
            return new a(o_aeh);
        }
        return new a(o_aeh, new aeh(j4, j3));
    }

    private void ˋ() {
        this.ʻ = 0;
        this.ˏॱ = 0;
    }

    private boolean ˋ(adv o_adv) throws IOException, InterruptedException {
        if (this.ˏॱ == 0) {
            if (!o_adv.ˊ(this.ᐝ.ˏ, 0, 8, true)) {
                return false;
            }
            this.ˏॱ = 8;
            this.ᐝ.ˋ(0);
            this.ʼ = this.ᐝ.ˋॱ();
            this.ʽ = this.ᐝ.ॱᐝ();
        }
        if (this.ʼ == 1) {
            o_adv.ˏ(this.ᐝ.ˏ, 8, 8);
            this.ˏॱ += 8;
            this.ʼ = this.ᐝ.ʼॱ();
        } else if (this.ʼ == 0) {
            long ॱ = o_adv.ॱ();
            if (ॱ == -1 && !this.ॱॱ.isEmpty()) {
                ॱ = ((o.aeu.e) this.ॱॱ.peek()).ॱᐧ;
            }
            if (ॱ != -1) {
                this.ʼ = (ॱ - o_adv.ˊ()) + ((long) this.ˏॱ);
            }
        }
        if (this.ʼ < ((long) this.ˏॱ)) {
            throw new ParserException("Atom size less than header length (unsupported).");
        }
        if (ˋ(this.ʽ)) {
            long ˊ = (o_adv.ˊ() + this.ʼ) - ((long) this.ˏॱ);
            this.ॱॱ.push(new o.aeu.e(this.ʽ, ˊ));
            if (this.ʼ == ((long) this.ˏॱ)) {
                ˎ(ˊ);
            } else {
                ˋ();
            }
        } else if (ˎ(this.ʽ)) {
            boolean z;
            if (this.ˏॱ == 8) {
                z = true;
            } else {
                z = false;
            }
            amm.ˏ(z);
            if (this.ʼ <= 2147483647L) {
                z = true;
            } else {
                z = false;
            }
            amm.ˏ(z);
            this.ˊॱ = new anc((int) this.ʼ);
            System.arraycopy(this.ᐝ.ˏ, 0, this.ˊॱ.ˏ, 0, 8);
            this.ʻ = 1;
        } else {
            this.ˊॱ = null;
            this.ʻ = 1;
        }
        return true;
    }

    private boolean ˊ(adv o_adv, adz o_adz) throws IOException, InterruptedException {
        boolean z;
        long j = this.ʼ - ((long) this.ˏॱ);
        long ˊ = o_adv.ˊ() + j;
        if (this.ˊॱ != null) {
            o_adv.ˏ(this.ˊॱ.ˏ, this.ˏॱ, (int) j);
            if (this.ʽ == aeu.ˊ) {
                this.ʽॱ = ॱ(this.ˊॱ);
                z = false;
            } else if (this.ॱॱ.isEmpty()) {
                z = false;
            } else {
                ((o.aeu.e) this.ॱॱ.peek()).ॱ(new d(this.ʽ, this.ˊॱ));
                z = false;
            }
        } else if (j < 262144) {
            o_adv.ॱ((int) j);
            z = false;
        } else {
            o_adz.ˊ = j + o_adv.ˊ();
            z = true;
        }
        ˎ(ˊ);
        if (!z || this.ʻ == 2) {
            return false;
        }
        return true;
    }

    private void ˎ(long j) throws ParserException {
        while (!this.ॱॱ.isEmpty() && ((o.aeu.e) this.ॱॱ.peek()).ॱᐧ == j) {
            o.aeu.e eVar = (o.aeu.e) this.ॱॱ.pop();
            if (eVar.ॱꓸ == aeu.ˊˊ) {
                ˋ(eVar);
                this.ॱॱ.clear();
                this.ʻ = 2;
            } else if (!this.ॱॱ.isEmpty()) {
                ((o.aeu.e) this.ॱॱ.peek()).ˏ(eVar);
            }
        }
        if (this.ʻ != 2) {
            ˋ();
        }
    }

    private void ˋ(o.aeu.e eVar) throws ParserException {
        Metadata metadata;
        Metadata ˊ;
        int i = -1;
        List arrayList = new ArrayList();
        aec o_aec = new aec();
        d ˊ2 = eVar.ˊ(aeu.ˌॱ);
        if (ˊ2 != null) {
            Metadata ˏ = aez.ˏ(ˊ2, this.ʽॱ);
            if (ˏ != null) {
                o_aec.ॱ(ˏ);
            }
            metadata = ˏ;
        } else {
            metadata = null;
        }
        o.aeu.e ˋ = eVar.ˋ(aeu.ˋʻ);
        if (ˋ != null) {
            ˊ = aez.ˊ(ˋ);
        } else {
            ˊ = null;
        }
        ArrayList ˊ3 = ˊ(eVar, o_aec, (this.ॱ & 1) != 0);
        int size = ˊ3.size();
        int i2 = 0;
        long j = -9223372036854775807L;
        while (i2 < size) {
            int size2;
            afi o_afi = (afi) ˊ3.get(i2);
            Track track = o_afi.ˎ;
            e eVar2 = new e(track, o_afi, this.ʻॱ.ˊ(i2, track.ˋ));
            eVar2.ˋ.ˎ(afa.ˎ(track.ˋ, track.ॱॱ.ˎ(o_afi.ॱ + 30), metadata, ˊ, o_aec));
            long max = Math.max(j, track.ˎ != -9223372036854775807L ? track.ˎ : o_afi.ᐝ);
            if (track.ˋ == 2 && i == -1) {
                size2 = arrayList.size();
            } else {
                size2 = i;
            }
            arrayList.add(eVar2);
            i2++;
            j = max;
            i = size2;
        }
        this.ॱˋ = i;
        this.ॱᐝ = j;
        this.ॱˎ = (e[]) arrayList.toArray(new e[arrayList.size()]);
        this.ᐝॱ = ˎ(this.ॱˎ);
        this.ʻॱ.ˊ();
        this.ʻॱ.ˎ(this);
    }

    private ArrayList<afi> ˊ(o.aeu.e eVar, aec o_aec, boolean z) throws ParserException {
        ArrayList<afi> arrayList = new ArrayList();
        for (int i = 0; i < eVar.ॱᐨ.size(); i++) {
            o.aeu.e eVar2 = (o.aeu.e) eVar.ॱᐨ.get(i);
            if (eVar2.ॱꓸ == aeu.ˊˋ) {
                Track ॱ = aez.ॱ(eVar2, eVar.ˊ(aeu.ˉ), -9223372036854775807L, null, z, this.ʽॱ);
                if (ॱ != null) {
                    afi ˎ = aez.ˎ(ॱ, eVar2.ˋ(aeu.ˋˋ).ˋ(aeu.ˋᐝ).ˋ(aeu.ˌ), o_aec);
                    if (ˎ.ˊ != 0) {
                        arrayList.add(ˎ);
                    }
                }
            }
        }
        return arrayList;
    }

    private int ॱ(adv o_adv, adz o_adz) throws IOException, InterruptedException {
        long ˊ = o_adv.ˊ();
        if (this.ˋॱ == -1) {
            this.ˋॱ = ˊ(ˊ);
            if (this.ˋॱ == -1) {
                return -1;
            }
        }
        e eVar = this.ॱˎ[this.ˋॱ];
        aef o_aef = eVar.ˋ;
        int i = eVar.ˊ;
        long j = eVar.ˎ.ˋ[i];
        int i2 = eVar.ˎ.ˏ[i];
        ˊ = (j - ˊ) + ((long) this.ॱˊ);
        if (ˊ < 0 || ˊ >= 262144) {
            o_adz.ˊ = j;
            return 1;
        }
        int ˏ;
        if (eVar.ˏ.ʻ == 1) {
            ˊ += 8;
            i2 -= 8;
        }
        o_adv.ॱ((int) ˊ);
        int i3;
        if (eVar.ˏ.ʼ != 0) {
            byte[] bArr = this.ˎ.ˏ;
            bArr[0] = (byte) 0;
            bArr[1] = (byte) 0;
            bArr[2] = (byte) 0;
            i3 = eVar.ˏ.ʼ;
            int i4 = 4 - eVar.ˏ.ʼ;
            while (this.ॱˊ < i2) {
                if (this.ͺ == 0) {
                    o_adv.ˏ(this.ˎ.ˏ, i4, i3);
                    this.ˎ.ˋ(0);
                    this.ͺ = this.ˎ.ʽॱ();
                    this.ˏ.ˋ(0);
                    o_aef.ˎ(this.ˏ, 4);
                    this.ॱˊ += 4;
                    i2 += i4;
                } else {
                    ˏ = o_aef.ˏ(o_adv, this.ͺ, false);
                    this.ॱˊ += ˏ;
                    this.ͺ -= ˏ;
                }
            }
            ˏ = i2;
        } else {
            while (this.ॱˊ < i2) {
                i3 = o_aef.ˏ(o_adv, i2 - this.ॱˊ, false);
                this.ॱˊ += i3;
                this.ͺ -= i3;
            }
            ˏ = i2;
        }
        o_aef.ˋ(eVar.ˎ.ʻ[i], eVar.ˎ.ʼ[i], ˏ, 0, null);
        eVar.ˊ++;
        this.ˋॱ = -1;
        this.ॱˊ = 0;
        this.ͺ = 0;
        return 0;
    }

    private int ˊ(long j) {
        long j2 = Long.MAX_VALUE;
        Object obj = 1;
        int i = -1;
        long j3 = Long.MAX_VALUE;
        long j4 = Long.MAX_VALUE;
        Object obj2 = 1;
        int i2 = -1;
        for (int i3 = 0; i3 < this.ॱˎ.length; i3++) {
            e eVar = this.ॱˎ[i3];
            int i4 = eVar.ˊ;
            if (i4 != eVar.ˎ.ˊ) {
                long j5 = eVar.ˎ.ˋ[i4];
                long j6 = this.ᐝॱ[i3][i4];
                j5 -= j;
                Object obj3 = (j5 < 0 || j5 >= 262144) ? 1 : null;
                if ((obj3 == null && r10 != null) || (obj3 == r10 && j5 < r12)) {
                    j3 = j6;
                    i = i3;
                    obj = obj3;
                    j2 = j5;
                }
                if (j6 < j4) {
                    i2 = i3;
                    obj2 = obj3;
                    j4 = j6;
                }
            }
        }
        return (j4 == Long.MAX_VALUE || obj2 == null || j3 < j4 + 10485760) ? i : i2;
    }

    private void ˋ(long j) {
        for (e eVar : this.ॱˎ) {
            afi o_afi = eVar.ˎ;
            int ˋ = o_afi.ˋ(j);
            if (ˋ == -1) {
                ˋ = o_afi.ˏ(j);
            }
            eVar.ˊ = ˋ;
        }
    }

    private static long[][] ˎ(e[] eVarArr) {
        int i;
        long[][] jArr = new long[eVarArr.length][];
        int[] iArr = new int[eVarArr.length];
        long[] jArr2 = new long[eVarArr.length];
        boolean[] zArr = new boolean[eVarArr.length];
        for (i = 0; i < eVarArr.length; i++) {
            jArr[i] = new long[eVarArr[i].ˎ.ˊ];
            jArr2[i] = eVarArr[i].ˎ.ʻ[0];
        }
        long j = 0;
        i = 0;
        while (i < eVarArr.length) {
            long j2 = Long.MAX_VALUE;
            int i2 = -1;
            int i3 = 0;
            while (i3 < eVarArr.length) {
                if (!zArr[i3] && jArr2[i3] <= r6) {
                    j2 = jArr2[i3];
                    i2 = i3;
                }
                i3++;
            }
            i3 = iArr[i2];
            jArr[i2][i3] = j;
            j += (long) eVarArr[i2].ˎ.ˏ[i3];
            i3++;
            iArr[i2] = i3;
            if (i3 < jArr[i2].length) {
                jArr2[i2] = eVarArr[i2].ˎ.ʻ[i3];
            } else {
                zArr[i2] = true;
                i++;
            }
        }
        return jArr;
    }

    private static long ˏ(afi o_afi, long j, long j2) {
        int ˊ = ˊ(o_afi, j);
        return ˊ == -1 ? j2 : Math.min(o_afi.ˋ[ˊ], j2);
    }

    private static int ˊ(afi o_afi, long j) {
        int ˋ = o_afi.ˋ(j);
        if (ˋ == -1) {
            return o_afi.ˏ(j);
        }
        return ˋ;
    }

    private static boolean ॱ(anc o_anc) {
        o_anc.ˋ(8);
        if (o_anc.ॱᐝ() == ˋ) {
            return true;
        }
        o_anc.ˎ(4);
        while (o_anc.ˊ() > 0) {
            if (o_anc.ॱᐝ() == ˋ) {
                return true;
            }
        }
        return false;
    }

    private static boolean ˎ(int i) {
        return i == aeu.ॱʻ || i == aeu.ˉ || i == aeu.ـ || i == aeu.ᐨ || i == aeu.ʼˋ || i == aeu.ʽˊ || i == aeu.ʾॱ || i == aeu.ॱͺ || i == aeu.ʼᐝ || i == aeu.ʽᐝ || i == aeu.ʽˋ || i == aeu.ˊʼ || i == aeu.ˊʻ || i == aeu.ॱʼ || i == aeu.ˊ || i == aeu.ˌॱ || i == aeu.ˎͺ || i == aeu.ˑॱ;
    }

    private static boolean ˋ(int i) {
        return i == aeu.ˊˊ || i == aeu.ˊˋ || i == aeu.ˋˋ || i == aeu.ˋᐝ || i == aeu.ˌ || i == aeu.ॱʽ || i == aeu.ˋʻ;
    }
}
